/*
 * @Author: wfl
 * @LastEditors: wfl
 * @description: 
 * @updateInfo: 本次更新内容：
 * @Date: 2021-04-28 14:38:57
 * @LastEditTime: 2021-04-29 13:35:34
 */
import axios from 'axios';
import { getType } from '/@/utils/getParamType'
import { useErrorAction, useInterceptor } from './axios_interceptor'
import { IRequest } from '/@/interface/if_rerquest'
import baseUrl from '/@/utils'
const source = axios.CancelToken.source();

interface FileStatus {
    fileExist: boolean;
    chunkList?: string[];
    desc: string;
}

// 默认公共配置
const defConfig = () => {
    const timeout = 5000;
    return {
        timeout,
        xsrfHeaderName: 'X-XSRF-TOKEN', // default
        xsrfCookieName: 'XSRF-TOKEN', // default
        // `maxContentLength` 定义允许的响应内容的最大尺寸
        maxContentLength: 2000,
        // 为上传处理进度事件
        onUploadProgress: (progressEvent: Event) => {
            // Do whatever you want with the native progress event
        },
        // 为下载处理进度事件
        onDownloadProgress: (progressEvent: Event) => {
            // 对原生进度事件的处理
        },
        // 取消请求 调用 source.cancel(param?) 可选参数
        // cancelToken: source.token
    }
}

/**
 * @description: GET请求处理
 * @param {(url) || (url, {xxx: xxx})} async
 * @return {*}
 */
export const Get = async (url: string, param?: Object): Promise<IRequest> => {
    let res: IRequest
    // 如果param参数是对象，转换为Query Params形式
    if (getType(param) === 'object') {
        const arr = Object.keys(param as Object);
        const len = arr.length - 1;
        arr.forEach((key, index) => {
            // 参数拼接
            url += `${index === 0 ? '?' : ''}${key}=${(<any>param)[key]}${index < len ? '&' : ''}`
        })
    };
    try {
        const response = await axios.get(baseUrl.apiUrl + url, { ...defConfig() });
        res = response?.data
    } catch (error) {
        res = await useErrorAction(error, 'GET')
    }
    return res
}

/**
 * @description: POST请求处理
 * @param {(url) || (url, {xxx: xxx})} async
 * @return {*}
 */
export const Post = async (url: string, param?: Object) => {

}